import axios from 'axios';
import store from '@/store';
const service = axios.create({
    // baseURL: process.env.VUE_APP_BASE_API,
    baseURL: 'http://h120.com:8086/cpisAPI',
    timeout: 5000
});

// 请求拦截器
service.interceptors.request.use(
    config => {
        const token = store.state.user.token;
        if (token) {
            config.headers['Authorization'] = `Bearer ${token}`;
        }
        return config;
    },
    error => {
        return Promise.reject(error);
    }
);

// 响应拦截器
service.interceptors.response.use(
    response => {
        if (response.data) {
            // 操作成功; 返回有效数据时,直接返回data
            return response.data;
        } else {
            // 操作失败,触发reject
            return Promise.reject(new Error('响应error!'));
        }
    },
    error => {
        return Promise.reject(error); // 返回执行错误 让当前的执行链跳出成功 直接进入catch
    }
);

export default service;
